import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ReadFile {
    public static String readCode(String filePath) {  //读取c--文件
        String content = "";
        try {
            content = new String(Files.readAllBytes(Paths.get(filePath)));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content;
    }

    public static List<LR1SyntaxAnalyzer.Production> readProductions(String filePath) {  //读取文法
        List<LR1SyntaxAnalyzer.Production> productions = new ArrayList<>();
        try {
            // 使用 Files 读取所有行
            List<String> lines = Files.readAllLines(Paths.get(filePath));
            // 遍历每一行并处理
            for (String line : lines) {
                List<String> productionStr = Arrays.asList(line.split(" "));
                productions.add(new LR1SyntaxAnalyzer.Production(productionStr.get(1), productionStr.subList(3, productionStr.size())));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return productions;
    }

    // 写入文件函数
    public static void writeToFile(String filePath, List<String> lines) {
        try {
            Files.write(Paths.get(filePath), lines);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        readProductions("input/syntax.txt");
    }
}